package com.tledu.production.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.system.SysRole;
import com.tledu.production.service.system.ISysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Antg
 * @date 2021/8/17  12:23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/role")
public class SysRoleController {
    private final ISysRoleService sysRoleService;

    @GetMapping("/list")
    public AjaxResult<PageInfo<SysRole>> findAll(Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = sysRoleService.findAll();
        PageInfo<SysRole> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

//    public AjaxResult<SysRole> findAllAvai

    @GetMapping("/search")
    public AjaxResult<PageInfo<SysRole>> search(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = sysRoleService.search(type, searchText);
        PageInfo<SysRole> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @PostMapping("/add")
    public AjaxResult add(SysRole sysRole) {
        int res = sysRoleService.add(sysRole);
        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }

    @PostMapping("/edit")
    public AjaxResult edit(SysRole sysRole) {
        int res = sysRoleService.edit(sysRole);
        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }

    @GetMapping("/del")
    public AjaxResult batchDel(String ids) {
        int res = sysRoleService.batchDel(ids.split(","));
        if (res >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }

    @GetMapping("/available")
    public AjaxResult available(@RequestParam("available") Boolean available, @RequestParam("roleId") String roleId) {
        int res = 0;
        if (available) {
            res = sysRoleService.available("0", roleId);
        } else {
            res = sysRoleService.available("1", roleId);
        }
        if (res == 1) {
            return new AjaxResult("请求成功");
        } else {
            return new AjaxResult("请求失败", 500);
        }
    }
}
